package com.project.service.goodsOrderService.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.project.bean.*;
import com.project.mapper.goodsOrderMapper.IGoodsOrderMapper;
import com.project.service.goodsOrderService.IGoodsOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

@Service
@Transactional
public class GoodsOrderServiceImpl implements IGoodsOrderService {
    @Autowired
    private IGoodsOrderMapper goodsOrderMapper;

    @Override
    public PageInfo<GoodsOrderBean> findAll(int pageNO, int storeId) {
        PageHelper.startPage(pageNO,PAGESIZE);
        return PageInfo.of(goodsOrderMapper.findAll(storeId));
    }

    @Override
    public void add(GoodsOrderBean goodsOrder, Map<Integer, ShopBean> shopMap, int userId) {
        List<ShopBean> shopList = new ArrayList<>(shopMap.values());
        Map<Integer,List<ShopBean>> newMap = new HashMap<>();
        for(ShopBean s : shopList){
            int storeId = s.getGoodsBean().getStore().getId();
            if(!newMap.containsKey(storeId)){
                List<ShopBean> newList = new ArrayList<>();
                newList.add(s);
                newMap.put(storeId, newList);
            }else {
                newMap.get(storeId).add(s);
            }
        }
        List<List<ShopBean>> mapList = new ArrayList<>(newMap.values());
        for(List<ShopBean> l : mapList){
            int totalPrice = 0;
            List<GoodsOrderInfoBean> infoList = new ArrayList<>();
            for(ShopBean s1 : l){
                GoodsOrderInfoBean goodsOrderInfo = new GoodsOrderInfoBean();
                totalPrice += s1.getNum()*s1.getGoodsBean().getPrice();
                goodsOrderInfo.setGoodsBean(s1.getGoodsBean());
                goodsOrderInfo.setNum(s1.getNum());
                goodsOrderInfo.setPrice(s1.getGoodsBean().getPrice());
                goodsOrderInfo.setTotalPrice(s1.getNum()*s1.getGoodsBean().getPrice());
                infoList.add(goodsOrderInfo);
            }
            goodsOrder.setStore(l.get(0).getGoodsBean().getStore());
            goodsOrder.setTotalPrice(totalPrice);
            UserBean user = new UserBean();
            goodsOrder.setUser(user);
            goodsOrder.getUser().setId(userId);
            StoreBean store = new StoreBean();
            goodsOrder.setStore(store);
            goodsOrder.getStore().setId(l.get(0).getGoodsBean().getStore().getId());
            goodsOrderMapper.add(goodsOrder);
            goodsOrderMapper.addOrderInfo(goodsOrder.getId(),infoList);
        }
    }

    @Override
    public void update(int orderId, String state) {
        goodsOrderMapper.update(orderId, state);
    }

    @Override
    public GoodsOrderBean findById(int orderId) {

        return goodsOrderMapper.findById(orderId);
    }
}
